/**
 * An object on the Chord ring.
 **/
package core1;

import java.util.Random;

import processing.core.PApplet;
import processing.core.PVector;

public class ChordObject implements Drawable, Physable, Chordable {
	
	protected PhysicalObject location;

	protected PApplet parent;
	protected Globals globals = Globals.getInstance();
	
	
	private long key;
	//private int maxKey = Integer.MAX_VALUE;
	private long maxKey = globals.maxKey;
    private float ringPosition;   // the logical position on the ring 
	private float size;
	
	
	public ChordObject(PApplet parent) {
		super();
		location = new PhysicalObject();
		size = 10; 
		//ringPosition = new Random().nextFloat()* 360;
		key =  Math.abs(new Random().nextLong()) % maxKey;
		ringPosition = (float)((key * 360) / maxKey);
		this.parent = parent;
		
	}
	
	/**
	 * Call the constructor but don't generate a random key, instead
	 * use the user specified key (if it's not larger than maxKey)
	 * 
	 * @param parent
	 * @param key
	 */
	public ChordObject(PApplet parent,long key) {
		
		super();
		location = new PhysicalObject();
		size = 10; 
		//ringPosition = new Random().nextFloat()* 360;
		this.key = key % maxKey; //Don't let the user specified key be larger than max!
		ringPosition = (float)((this.key * 360) / maxKey);
		this.parent = parent;
	}
	
	public boolean mouseOver() {
		PVector mouse = globals.zoomer.getMouseCoord();
		return Math.pow(mouse.x-getXpos(),2)+Math.pow(mouse.y-getYpos(),2) < Math.pow(size/globals.scale,2);
	}
	
	/**
	 * Calculates a mouse distance score that can be used to compare
	 * the distance between the mouse pointer and different objects. 
	 * The score is actually ((mx-x)^2 + (my-y)^2), where mx and my is the 
	 * position of the mouse pointer, and x and y is the position of the object.
	 **/
	public double mouseDistanceScore() {
		PVector mouse = globals.zoomer.getMouseCoord();
		return Math.pow(mouse.x-getXpos(),2)+Math.pow(mouse.y-getYpos(),2);
	}
	
	public void draw(float scale) {
	  location.update();
	 
 	  //parent.pushMatrix();
	  float x = location.getXpos();
	  float y = location.getYpos();
	  
	  parent.translate(x,y);
	  parent.ellipse(0, 0, size, size);
	  parent.text(key, x, y);
	  
	}

	public void setLogicView()
	{
		double r = Globals.radius;
		
		float newx = (float) r* (float) Math.cos(Math.toRadians(getRingPosition()));
		float newy = (float) r* (float) Math.sin(Math.toRadians(getRingPosition()));
		setTarget(newx, newy);

	}

	@Override
	public float getRingPosition() {
return ((float)key /(float)maxKey) *360f ;
	}
	
	public float getRingPosition(long key) {
return ((float)key /(float)maxKey) *360f ;
	}


	@Override
	public float getXpos() {
		// TODO Auto-generated method stub
		return location.getXpos();
	}



	@Override
	public float getYpos() {
		// TODO Auto-generated method stub
		return location.getYpos();
	}



	@Override
	public void setPhysics(float mass, float k, float damp) {
		location.setPhysics(mass, k, damp);
		
	}



	@Override
	public void setTarget(float x, float y) {
		location.setTarget(x, y);
		
		
	}



	@Override
	public void update() {
		location.update(); 
		
	}



	@Override
	public long getKey() {
		// TODO Auto-generated method stub
		return key;
	}



	@Override
	public long getMaxKey() {
		// TODO Auto-generated method stub
		return maxKey;
	}
	
	@Override
	public int compareTo(Chordable o) {
		return (key>=o.getKey())?-1:1;
		
		
	}


}
